//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.Diagnostics.Contracts;
using System.Security;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Diagnostics;
using OpenLS.UI;

namespace OpenLS.Drawing.UI
{
    public partial  class LineResources
    {
        public LineResources()
        {
            InitializeComponent();
        }
// ReSharper disable UnusedPrivateMember
#pragma warning disable 168
        public Delegate _CreateDelegate (Type delegateType, string handler)
#pragma warning restore 168
// ReSharper restore UnusedPrivateMember
        {
            switch (handler)
            {
                case "Width_ValueChanged":
                    return new RoutedPropertyChangedEventHandler<double>(Width_ValueChanged);
                    default:
                    throw new NotSupportedException();
            }
        }

        void OnRadioButtonChecked(object sender, RoutedEventArgs args)
        {
            if (loading)
                return;
            var element = (FrameworkElement) sender;
            var sp = (ShapeProperties)(element).DataContext;
            if (sp == null)
                return;
            var rb = (FrameworkElement) args.Source;
            if (sp.LineProperties == null)
                sp.LineProperties = new LineProperties ();
            LineProperties lp = sp.LineProperties;
            switch (rb.Name)
            {
                case "noFill":
                    lp.LineFillProperties = new NoFillProperties();
                    break;
                case "automatic":
                    lp.LineFillProperties = null;
                    break;
                case "solidFill":
                    var s = (Selector) rb.Tag ;
                    if (s.SelectedIndex == -1)
                        s.SelectedIndex = 0;
                    var cb = (ColorBase) s.SelectedItem ;
                    lp.LineFillProperties = new SolidColorFillProperties(cb);
                    break;
               
            }
        }

        private bool loading;
        void OnLoaded(object sender, object args)
        {
            loading = true;
            try
            {
                var sp = (ShapeProperties)((FrameworkElement)sender).DataContext;
                if (sp == null)
                    return;
                if (sp.LineProperties == null)
                {
                    GetRadioButton("automatic", sender).IsChecked = true;
                    return;
                }
                LineFillProperties lf = sp.LineProperties.LineFillProperties;
                if (lf == null)
                {
                    GetRadioButton("automatic", sender).IsChecked = true;
                    return;
                }
                if (lf is NoFillProperties)
                {
                    GetRadioButton("noFill", sender).IsChecked = true;
                    return;
                }
                if (lf is SolidColorFillProperties)
                {
                    GetRadioButton("solidFill", sender).IsChecked = true;
                    return;
                }
                if (lf is GradientFillProperties)
                {
                    GetRadioButton("gradientFill", sender).IsChecked = true;
                    return;
                }
                if (lf is PatternFillProperties)
                {
                    GetRadioButton("pictureFill", sender).IsChecked = true;
                    return;
                }
                // Contract.Assert(false);
            }
            finally
            {
                loading = false;
            }
        }

        static RadioButton GetRadioButton(string name, object sender)
        {
            var cp = (ContentPresenter)((FrameworkElement)sender).TemplatedParent;
            DataTemplate dt = cp.ContentTemplate;
            return dt.FindName(name, cp) as RadioButton;
        }
        void OnSolidFillChanged(object sender, SelectionChangedEventArgs args)
        {
            var fSender = (FrameworkElement)sender;
            if (fSender.Visibility != Visibility.Visible)
                return;
            var rb = fSender.Tag as RadioButton;
            if (rb == null || !rb.IsChecked.GetValueOrDefault())
                return;
            var c = new SolidColorFillProperties((ColorBase)args.AddedItems[0]);
            var s = (ShapeProperties)fSender.DataContext;
            if (s != null)
                s.LineProperties.LineFillProperties = c;
        }

        public void Width_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            ShapeProperties p = DrawingElement.GetShapeProperties((DependencyObject) sender);
            if (p.LineProperties == null)
                p.LineProperties = new LineProperties();
            p.LineProperties.Width = e.NewValue;
        }

        private void Width_Loaded(object sender, RoutedEventArgs e)
        {
            ShapeProperties p = DrawingElement.GetShapeProperties((DependencyObject)sender);
            if (p.LineProperties != null && p.LineProperties.Width != null)
                ((RangeBase) sender).Value = p.LineProperties.Width.Value;

        }

        private void DashArray_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            GetEnsuredLineProperties(sender).LineDashProperties = new PresetLineDashProperties {Value = (PresetLineDashValue)e.AddedItems[0]};
            e.Handled = true;

        }
        static ShapeProperties GetShapeProperties (object sender)
        {
            return DrawingElement.GetShapeProperties((DependencyObject) sender);
        }
        static LineProperties GetEnsuredLineProperties (object sender)
        {
            var p = GetShapeProperties(sender);
            if (p.LineProperties == null)
                p.LineProperties = new LineProperties();
            return p.LineProperties;
        }

        private void LineCap_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            GetEnsuredLineProperties(sender).LineCap = (LineCap) e.AddedItems[0];
            e.Handled = true;
        }
        private void Grid_Loaded(object sender, RoutedEventArgs e)
        {
            var grid = (Grid) sender;
            var control = (NumericUpDown)grid.FindName("updown");
            ShapeProperties p = DrawingElement.GetShapeProperties((DependencyObject)sender);
            if (p.LineProperties != null && p.LineProperties.Width != null)
                control.Value = p.LineProperties.Width.Value;
            control.ValueChanged += Width_ValueChanged;

        }
    }
}
